package com.guo.rabbitmq.quickstart;

import com.rabbitmq.client.*;

import java.io.IOException;


import java.util.concurrent.TimeoutException;

/**
 * @author guo
 * @date 2021/11/17 10:04 星期三
 **/
public class RabbitMqConsumer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.80.101");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //获取连接
        Connection connection = connectionFactory.newConnection();


        //创建channel
        Channel channel = connection.createChannel();

        String queueName = "test001";
        //声明队列
        channel.queueDeclare(queueName, true, false, false, null);

        boolean autoAck = true;
        channel.basicConsume(queueName, true,"",
                new DefaultConsumer(channel){
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                        long deliveryTag = envelope.getDeliveryTag();
                        java.lang.String routingKey = envelope.getRoutingKey();
                        java.lang.String exchange = envelope.getExchange();
                        System.out.println(deliveryTag);
                        System.out.println(routingKey);
                        System.out.println(exchange);
                        System.out.println(properties.toString());
                        String msg = new String(body);
                        System.out.println(msg);
                    }
                });

        channel.close();
        connection.close();

    }
}
